package com.llh.demo4tree.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import kotlin.streams.toList

data class ResponseVO(
    val success: Boolean = true,
    val msg: String = "ok",
)

data class NodeAddVO(
    val name: String,
    val nodePath: String? = null,
    val pId: Int? = null,
)

data class TreeVO(
    val id: Int?,
    val name: String,
    val orderNum: Int,
    val nodePath: String? = null,
    val pId: Int? = null,
    @field:JsonIgnore
    val childList: MutableList<TreeVO> = mutableListOf(),
    /**
     * 配合ztree组件的属性
     */
    val open: Boolean = false,
) {
    /**
     * 配合ztree组件的属性
     */
    fun getIsParent(): Boolean {
        return this.childList.isNotEmpty()
    }

    /**
     * 在序列化时进排序
     */
    fun getChildren(): List<TreeVO> {
        return childList.stream().sorted(
            Comparator.comparing { it.orderNum }
        ).toList()
    }

}

/**
 * 移动节点所需数据
 */
data class TreeNodeMoveVO(
    val sourceId: Int,
    val sourcePid: Int?,
    val targetId: Int?,
    val direction: MoveDirection,
)

/**
 * 节点移动的方向
 */
enum class MoveDirection {
    /**
     * 移动目标节点的内部。即成为其子节点
     */
    inner,

    /**
     * 移动到目标节点之前。
     */
    prev,

    /**
     * 移动到目标节点之后。
     */
    next
}